package org.awenlice.playerdailyshop.gui;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.network.NetworkHooks;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.awenlice.playerdailyshop.config.ShopConfig;
import org.awenlice.playerdailyshop.data.PlayerBuyShopData;
import org.awenlice.playerdailyshop.data.PlayerBuyShopDataManager;
import org.awenlice.playerdailyshop.util.CrossModItemLoader;
import org.awenlice.playerdailyshop.util.RandomItemGenerator;
import java.util.ArrayList;
import java.util.List;

public class BuyShopGui {
    private static final Logger LOGGER = LogManager.getLogger();

    public static void open(Player player) {
        if (player instanceof ServerPlayer) {

            ServerPlayer serverPlayer = (ServerPlayer) player;

            // 获取玩家的收购商店数据
            PlayerBuyShopData shopData = PlayerBuyShopDataManager.get(serverPlayer);
            List<ItemStack> buyShopItems = shopData.getBuyShopItems();

            // 如果收购商店数据为空，则生成新的数据
            if (buyShopItems.isEmpty()) {
                buyShopItems = generateBuyShopItems();
                shopData.setBuyShopItems(buyShopItems);
            }
            // 创建一个包含27个槽位的SimpleContainer
            SimpleContainer inventory = new SimpleContainer(27);
            for (int i = 0; i < 27; i++) {
                inventory.setItem(i, buyShopItems.get(i));
            }

            // 创建一个SimpleMenuProvider来打开GUI
            SimpleMenuProvider provider = new SimpleMenuProvider(
                    (windowId, playerInventory, playerEntity) -> new BuyShopMenu(windowId, playerInventory, inventory),
                    Component.translatable("pds.buyShop")
            );

            // 打开GUI
            NetworkHooks.openScreen(serverPlayer, provider);

        }
    }

    /**
     * 生成新的收购商店数据
     */
    public static List<ItemStack> generateBuyShopItems() {
        List<ItemStack> buyShopItems = new ArrayList<>();
        for (int i = 0; i < 27; i++) {
            ShopConfig.ShopItem item = RandomItemGenerator.getRandomItem("Buy");
            if (item != null) {
                String modId = item.getId().split(":")[0];
                String itemName = item.getId().split(":")[1];
                ItemStack stack = CrossModItemLoader.getItemStackFromOtherMod(modId, itemName);
                // 日志输出
                if (stack != null && !stack.isEmpty()) {
                    addTooltip(stack, item);
                    buyShopItems.add(stack);
                }
            }
        }
        return buyShopItems;
    }

    /**
     * 为ItemStack添加自定义提示信息
     */
    private static void addTooltip(ItemStack stack, ShopConfig.ShopItem item) {
        // 应用 NBT 数据
        CompoundTag nbt = item.getNbt();
        if (!nbt.isEmpty()) {
            stack.setTag(nbt);
        }
        stack.getOrCreateTag().putLong("price", item.getPrice());
        stack.getOrCreateTag().putString("weight", item.getWeight());
//        LOGGER.warn(stack.getOrCreateTag().get("quality_food")); // 日志输出
//        LOGGER.warn(stack.getOrCreateTag().toString()); // 日志输出
    }
}